package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);

        Page<Role> page = roleDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public void add(Integer[] permissionIds, Integer[] menuIds, Role role) {
        roleDao.add(role);
        setRoleAndPermission(permissionIds,role);
        setRoleAndMenu(menuIds,role);
    }

    @Override
    public Role findById(int id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(int id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(int id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

    @Override
    public void edit(Integer[] permissionIds, Integer[] menuIds, Role role) {
        roleDao.edit(role);
        roleDao.deleteAssocPermission(role.getId());
        roleDao.deleteAssocMenu(role.getId());
        setRoleAndPermission(permissionIds,role);
        setRoleAndMenu(menuIds,role);
    }

    @Override
    public void deleteById(Integer id) {
        roleDao.deleteAssocPermission(id);
        roleDao.deleteAssocMenu(id);
        roleDao.delete(id);
    }

    public void setRoleAndMenu(Integer[] menuIds, Role role) {
        Integer roleId = role.getId();
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    public void setRoleAndPermission(Integer[] permissionIds, Role role) {
        Integer roleId = role.getId();
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

}
